package com.liupengyuan.one.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.liupengyuan.framework.web.query.IGenericPage;
import com.liupengyuan.one.dao.TreeDao;
import com.liupengyuan.one.model.Tree;
import com.liupengyuan.one.service.TreeService;

@Service("treeService")
public class TreeServiceImpl implements TreeService {
	@Autowired
	private TreeDao treeDao;
	

	public List<Tree> findByparentId(String parentId) {
		Tree t = new Tree();
		if(StringUtils.isEmpty(parentId)){
			t.setId("root");
			t.setName("树根");
			t.setNote("屡有");
			List<Tree> list = new ArrayList<Tree>(1);
			list.add(t);
			return list;
		}else{
			t.setParentNodeId(parentId);
			return treeDao.findListBy(t,"sort","asc");
		}
	}


	public IGenericPage findPageBy(Tree tree, int pageNo, int pageSize,
			String sort, String dir) {
		return treeDao.findPageBy(tree, pageNo, pageSize, sort, dir);
	}

	public Tree getById(String id) {
		return (Tree) treeDao.getById(id);
	}

	public Integer save(Tree tree) {
		return treeDao.save(tree);
	}

	public Integer update(Tree tree){
		return treeDao.update(tree);
	}

	public Integer deleteByIds(String[] ids) {
		return treeDao.deleteByIds(ids);
	}
	public Integer deleteById(String id) {
		return treeDao.deleteById(id);
	}

	public Integer getCountByParam(Tree tree) {
		return treeDao.getCountBy(tree);
	}

	public Integer saveOrUpdate(Tree tree) {

		if(tree!=null){
			if(StringUtils.isNotEmpty(tree.getId())){
				return treeDao.update(tree);
			}else{
				return treeDao.save(tree);
			}
		} else {
			throw new RuntimeException("����Ϊnull");
		}
	}

	public List<Tree> findListBy(Tree tree, String sort, String dir) {
		return treeDao.findListBy(tree, sort, dir);
	}

}
